
//
//  SYSymbolKeyboard.m
//  SYKeyboard
//
//  Created by hc on 2019/3/22.
//  Copyright © 2019 hc. All rights reserved.
//

#import "SYSymbolKeyboard.h"

@implementation SYSymbolKeyboard

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        if (self = [super initWithFrame:frame]) {
            [self setupSubviews];
        }
        return self;
    }
    return self;
}

/**
 快速创建符号键盘
 
 @param delegate 代理对象
 @return 键盘对象
 */
+ (SYSymbolKeyboard *)creatLetterKeyboardWithDelegateTarget:(id<SYBaseKeyboardDelegate>)delegate {
    SYSymbolKeyboard * keyboard = [[SYSymbolKeyboard alloc] initWithFrame:CGRectMake(0, 0, SYKeyboardScreenWidth, SYKeyboardHeight)];
    keyboard.delegate = delegate;
    return keyboard;
}

- (void)setupSubviews {
    int row = 4;
    int coloumn = 10;
    int width = 5;
    int height = 10;
    
    CGFloat keyWidth = (SYKeyboardScreenWidth - width*11) / coloumn;
    CGFloat keyHeight = (SYKeyboardHeight - height*5) / row;
    
    NSString * str = @"~ ` ! @ # $ % ^ & * ( ) _ - + = { } [ ] | \\ : ; “ ‘ < ， > ? 123 abc . / space € ￡ ¥ back";
    NSArray * symbolArr = [str componentsSeparatedByString:@" "];

    
    for (int i = 0; i < symbolArr.count; i++) {
        UIButton * bt = [[UIButton alloc] initWithFrame:CGRectMake((i%10) * (keyWidth+width) + width, height + (keyHeight +height)*(i/10), keyWidth, keyHeight)];
        bt.tag = i;
        [bt setBackgroundImage:[[UIImage imageNamed:@"zimubaikuang"] stretchableImageWithLeftCapWidth:10 topCapHeight:10] forState:UIControlStateNormal];
        [bt setTitle:symbolArr[i] forState:UIControlStateNormal];
        [bt setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        bt.titleLabel.font = [UIFont systemFontOfSize:18];
        [self addSubview:bt];
        [bt addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 34)  {
            bt.frame = CGRectMake((i%10) * (keyWidth+width) + width, height + (keyHeight +height)*(i/10), keyWidth + (keyWidth+width), keyHeight);
            [bt setTitle:@"空格" forState:UIControlStateNormal];
        }
        
        if (i > 34) bt.frame = CGRectMake(((i%10)+1) * (keyWidth+width) + width, height + (keyHeight +height)*(i/10), keyWidth, keyHeight);
        
        if (i == 38) {
            [bt setBackgroundImage:nil forState:UIControlStateNormal];
            [bt setImage:[UIImage imageNamed:@"icon_del_black"] forState:UIControlStateNormal];
            [bt setTitle:@" " forState:UIControlStateNormal];
        }
    }
}

- (void)buttonTap:(UIButton *)sender{
    switch (sender.tag) {
        case 30:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"123" keyType:NumberSwitchKeyType keyboardType:SymbolKeyboard];
            }
        }
            break;
        case 31:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"abc" keyType:LetterSwitchKeyType keyboardType:SymbolKeyboard];
            }
        }
            break;
        case 34:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@" " keyType:SymbolKeyType keyboardType:SymbolKeyboard];
            }
        }
            break;
        case 38:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"删除" keyType:DeleteKeyType keyboardType:SymbolKeyboard];
            }
        }
            break;
            
        default:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:sender.titleLabel.text keyType:SymbolKeyType keyboardType:SymbolKeyboard];
            }
        }
            break;
    }
    
}

@end
